/**
 * Alert the booking administrator of an identified problem with this booking.
 */
function sendAdminMail_(booking, errs) {
  Logger.log("Send alert to admin: " + errs.toString());
  
  var recipient = "ellesboroughguidecentre@gmail.com";
  var bookingDateStr = booking.arrivalDate.toDateString();
  var bookingOrgName = booking.organisation.name;
  
  var errsStr = "";
  for(i = 0; i < errs.length; i ++) {
    errsStr += " -" + errs[i] + "\n";
  }
  
  var subject = "Action required: booking failure from " + bookingOrgName + " for " + bookingDateStr;
  
  var body = "!! ACTION REQUIRED !!\n\n" +
    "A booking received from " + bookingOrgName + " starting on " + bookingDateStr + " has been rejected for the reasons below:\n" +
    errsStr + "\n" +
    "Please contact the booker " + booking.contact.name + " (" + booking.contact.telephone + " / " + booking.contact.email + ") to discuss their booking.\n\n" +
    "Full booking details follow:\n\n" +
    JSON.stringify(booking, null, 2) + "\n";
  
  var options = {};
  
  MailApp.sendEmail(recipient, subject, body, options);    
}

function sendCancelledEmail_(booking) {
  
  var recipient = booking.contact.email;
  
  var subject = "Your Ellesborough booking for " + booking.arrivalDate.toDateString() + " has been cancelled";
  
  //MailApp.sendEmail(recipient, subject, body, options);
  //Logger.log("Mail [cancelledMail] sent to %s", recipient);
}

function sendConfirmedEmail_(booking) {
  
  var recipient = booking.contact.email;
  var bookingDateStr = booking.arrivalDate.toDateString();
  
  var subject = "Your Ellesborough booking for " + bookingDateStr + " - confirmation";
  
  var body = "Dear " + booking.contact.name + "\n" +
    "We are pleased to inform you that your Ellesborough booking for " + bookingDateStr + " has been confirmed and is recorded in the bookings calendar.\n";
  
  body += getEventDescription_(booking) + "\n";
  body += booking.organisation.name + " (" + booking.organisation.type + ")\n";
  body += "Event type:\t\t" + booking.eventType + "\n";
  body += "Expected participants:\t" + (booking.participants + booking.leaders) + "\n";
  
  if(booking.isFieldBooking) {
    if(booking.equipment.fullyEquipped) {
      body += "Including full campsite equipment.\n";
    }
    if(booking.equipment.tents > 0) {
      body += "Tent hire:\t" + booking.equipment.tents + "\n";
    }
  }
  
  body +=  "\nYours,\nEllesborough Bookings.";
  
  var options = {};
  
  if(!isTestBooking_(booking)) {
    MailApp.sendEmail(recipient, subject, body, options);
    Logger.log("Mail [confirmedMail] sent to %s", recipient);
  } else {
    Logger.log("Mailto: %s; %s", recipient, body);
  }
}

function sendProvisionalEmail_(booking) {
  
  var recipient = booking.contact.email;
  var bookingDateStr = booking.arrivalDate.toDateString();
  
  var subject = "Your Ellesborough booking for " + bookingDateStr + " - provisional booking";
  
  var body = "Dear " + booking.contact.name + "\n" +
    "Thank you for making a booking at Ellesborough Guide Centre on " + bookingDateStr + ".\n";
  
  var options = {};
  var invoiceTotal = booking.invoice.total;
  if(invoiceTotal > 0) {
    body += "To complete your booking, payment is now due; your invoice is attached.\n";
    
    var invoice = DriveApp.getFileById(booking.docs.invoiceID);
    options = { "attachments" : [ invoice.getAs(MimeType.PDF) ] };
    
  } else {
    body += "As your booking is a non-chargable activity, no payment is due and your booking will be confirmed shortly.\n";
  }
  
  body += "Yours,\nEllesborough Bookings.";
  
  if(!isTestBooking_(booking)) {
    MailApp.sendEmail(recipient, subject, body, options);
    Logger.log("Mail [provisionalMail] sent to %s", recipient);
  } else {
    Logger.log("Mailto: %s; %s", recipient, body);
  }
}

function sendReminder1Email_(booking) {
  
  var recipient = booking.contact.email;
  var bookingDateStr = booking.arrivalDate.toDateString();
  
  var subject = "Your Ellesborough booking for " + bookingDateStr + " - payment due";
  
  var body = "Dear " + booking.contact.name + "\n" +
    "Thank you for making a booking at Ellesborugh Guide Centre on " + bookingDateStr + ".\n" +
    "This is a reminder that a payment in relation to your booking is now due, please ensure this is made as soon as possible to prevent your booking from being cancelled.\n" +
    "Yours,\nEllesborough Bookings.";
  
  var options = {};
  
  if(!isTestBooking_(booking)) {
    MailApp.sendEmail(recipient, subject, body, options);
    Logger.log("Mail [reminder1Mail] sent to %s", recipient);
  } else {
    Logger.log("Mailto: %s; %s", recipient, body);
  }

}

function sendReminder2Email_(booking) {
  
  var recipient = booking.contact.email;
  var bookingDateStr = booking.arrivalDate.toDateString();
  
  var subject = "Your Ellesborough booking for " + bookingDateStr + " - payment overdue";
  
  var body = "Dear " + booking.contact.name + "\n" +
    "Thank you for making a booking at Ellesborugh Guide Centre on " + bookingDateStr + ".\n" +
    "This is a reminder that a payment in relation to your booking is now overdue, please ensure this is made immediately to prevent your booking from being cancelled.\n" +
    "Yours,\nEllesborough Bookings.";
  
  var options = {};
  
  if(!isTestBooking_(booking)) {
    MailApp.sendEmail(recipient, subject, body, options);
    Logger.log("Mail [reminder2Mail] sent to %s", recipient);
  } else {
    Logger.log("Mailto: %s; %s", recipient, body);
  }

}



function sendReviewedEmail_(booking) {
  
  var recipient = booking.contact.email;
  var bookingDateStr = booking.arrivalDate.toDateString();
  
  var subject = "Your Ellesborough booking for " + bookingDateStr + " has been received";
  
  var body = "Dear " + booking.contact.name + "\n" +
    "Thank you for making a booking at Ellesborough Guide Centre on " + bookingDateStr + ".\n" +
    "Your booking request has been received and is now being reviewed. We will be in touch shortly to discuss your booking.\n" +
    "Yours,\nEllesborough Bookings.";
  
  var options = { };
  
  if(!isTestBooking_(booking)) {
    MailApp.sendEmail(recipient, subject, body, options);
    Logger.log("Mail [reviewedMail] sent to %s", recipient);
  } else {
    Logger.log("Mailto: %s; %s", recipient, body);
  }
  
}
